﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGS.Cards;
using SGS.Standard.Delegate;
using SGS.Standard.Actor;
using SGS.Standard.EventArgs;
using SGS.Standard.CardSelector;

namespace SGS.Standard.GameLogic
{
    class Ask : GameLogicBase
    {
        #region Core - 问询动作
        internal static void GL_AskWuXieKeJi()
        {
        }

        /// <summary>
        /// 牌弃至牌堆需另外自行实现
        /// </summary>
        /// <param name="answerPlayer"></param>
        /// <param name="callback"></param>
        /// <param name="fromActor"></param>
        /// <returns></returns>
        internal static AskUseCardResult GL_AskUseCard(Player answerPlayer, AskUsedCardCallback callback, ActorBase fromActor = null)
        {
            HumanActorEventArgs eq = new HumanActorEventArgs(answerPlayer, fromActor);
            HumanAnswerActorEventArgs ea = new HumanAnswerActorEventArgs();

            while (ea.AnswerActor == null)
            {
                RaiseHumanActorEvent(eq, ea);

                if (ea.AnswerActor is SkipActor)
                {
                    if (GameContext.Current.EnableSkipActor)
                    {
                        return AskUseCardResult.SkipResult;
                    }
                    else
                    {
                        continue;
                    }
                }
                else if (ea.AnswerActor is CancelActor)
                {
                    return AskUseCardResult.CancelResult;
                }
                else if (ea.AnswerActor is SGS.Standard.Actor.HumanAnswerActor.UseCardActor)
                {
                    var answer = ea.AnswerActor as SGS.Standard.Actor.HumanAnswerActor.UseCardActor;
                    if (ea.AnswerActor.CheckHumanActor(eq))
                    {
                        bool result = callback(answerPlayer, answer);
                        if (result)
                        {
                            // 从手牌中移除
                            answerPlayer.GameInfo.TryRemoveCard(answer.UsedCards);

                            return new AskUseCardResult(AskUseCardResult.AskResult.Used, answer.UsedCards);
                        }
                        else
                        {
                            ea.AnswerActor = null;
                        }
                    }
                    else
                    {
                        // 此处为应答回来的内容错误
                        ea.AnswerActor = null;
                    }
                }
            }

            return AskUseCardResult.CancelResult;
        }

        internal static AskUseCardResult GL_AskMiss(Player answerPlayer, ActorBase fromActor = null)
        {
            RaiseNoticeMessageEvent("询问{0}是否出闪", answerPlayer.PlayerName);
            return GL_AskUseCard(answerPlayer, (player, answerActor ) =>
                {
                    return answerActor.AsSkillType == CardType.Miss;
                }, fromActor);

        }

        internal static AskUseCardResult GL_AskPeach(Player fromPlayer, Player answerPlayer, int peachCount = 1)
        {
            RaiseNoticeMessageEvent("{0}向{1}求{2}个桃", fromPlayer.PlayerName, answerPlayer.PlayerName, peachCount);
            return GL_AskUseCard(answerPlayer, (player, answerActor) =>
            {
                return answerActor.AsSkillType == CardType.Peach;
            } );
        }

        internal static AskUseCardResult GL_AskBang(Player answerPlayer, ActorBase fromActor = null)
        {
            RaiseNoticeMessageEvent("询问{0}是否出杀", answerPlayer.PlayerName);
            return GL_AskUseCard(answerPlayer, (player, answerActor) =>
            {
                return answerActor.AsSkillType == CardType.Bang || answerActor.AsSkillType == CardType.FireBang || answerActor.AsSkillType == CardType.ThunderBang;
            });
        }



        //internal static T GL_AskHumanActor<T>(Player answerPlayer, AskHumanActorCallback<T> mainCallback,
        //    AskHumanActorCallback<T> cancelCallback = null,
        //    AskHumanActorCallback<T> skipCallback = null, 
        //    ActorBase fromActor = null)
        //{
        //    HumanActorEventArgs eq = new HumanActorEventArgs(answerPlayer, fromActor);
        //    HumanAnswerActorEventArgs ea = new HumanAnswerActorEventArgs();

        //    while (ea.AnswerActor == null)
        //    {
        //        RaiseHumanActorEvent(eq, ea);
                
        //        if (ea.AnswerActor is SkipActor)
        //        {
        //            if( GameContext.Current.EnableSkipActor )
        //            {
        //                skipCallback( answerPlayer, eq,ea );
        //            }
        //        }
        //        else if (ea.AnswerActor is CancelActor)
        //        {
        //            cancelCallback(answerPlayer,eq,ea);
        //        }
        //        else
        //        {
        //            if (!mainCallback(answerPlayer, eq, ea))
        //            {
        //                ea.AnswerActor = null;
        //            }
        //        }
        //    }
        //}


        internal static CardSelectorMappingItem GL_AskSelectSomeCard(Player answerPlayer, CardSelectorMapping mapping)
        {
            SelectOtherCardsActor fromActor = new SelectOtherCardsActor(mapping);

            HumanActorEventArgs eq = new HumanActorEventArgs(answerPlayer, fromActor);
            HumanAnswerActorEventArgs ea = new HumanAnswerActorEventArgs();

            RaiseNoticeMessageEvent("询问{0}选择一张牌", answerPlayer.PlayerName);
            while (ea.AnswerActor == null)
            {
                RaiseHumanActorEvent(eq, ea);

                if (ea.AnswerActor is SkipActor)
                {
                    return null;
                }
                else if (ea.AnswerActor is CancelActor)
                {
                    return mapping.MappingResult.FirstOrDefault();
                    // break;
                }
                else if (ea.AnswerActor is SGS.Standard.Actor.HumanAnswerActor.SelectOtherCardsAnswerActor)
                {
                    var answer = ea.AnswerActor as SGS.Standard.Actor.HumanAnswerActor.SelectOtherCardsAnswerActor;

                    if (ea.AnswerActor.CheckHumanActor(eq))
                    {
                        var selectedItem = mapping.MappingResult.Where(p => p.UniqueID == answer.SelectedID).FirstOrDefault();
                        if (selectedItem == null)
                        {
                            ea.AnswerActor = null;
                        }
                        else
                        {
                            return selectedItem;
                        }
                    }
                    else
                    {
                        ea.AnswerActor = null;
                    }
                }
            }

            return null;
        }
        #endregion
    }
}
